package com.ruoyi.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.TenementsaskMapper;
import com.ruoyi.mapper.UcleictaskMapper;
import com.ruoyi.service.CommService;
import com.ruoyi.service.IUcleictaskService;
import com.ruoyi.service.RidService;
import com.ruoyi.service.TeneMentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 楼栋核酸Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-23
 */
@Service
@Slf4j
public class UcleictaskServiceImpl implements IUcleictaskService 
{
    @Autowired
    private UcleictaskMapper ucleictaskMapper;

    @Autowired
    private CommService commService;

    @Autowired
    private RidService ridService;
    @Autowired
    private TeneMentService teneMentService;

    @Autowired
    private TenementsaskMapper tenementsaskMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询楼栋核酸
     * 
     * @param id 楼栋核酸主键
     * @return 楼栋核酸
     */
    @Override
    public Ucleictask selectUcleictaskById(Long id)
    {
        return ucleictaskMapper.selectUcleictaskById(id);
    }

    /**
     * 查询楼栋核酸列表
     * 
     * @param ucleictask 楼栋核酸
     * @return 楼栋核酸
     */
    @Override
    public List<Ucleictask> selectUcleictaskList(Ucleictask ucleictask)
    {
        List<Ucleictask> ucleictasks = ucleictaskMapper.selectUcleictaskList(ucleictask);
        ucleictasks.forEach(u->{
            Long communityId = u.getCommunityId();
            Long ridgepoleId = u.getRidgepoleId();
            AjaxResult info1 = commService.getInfo(communityId);
            Object o = info1.get(AjaxResult.DATA_TAG);
            Community community = JSON.parseObject(JSON.toJSONString(o), Community.class);

            AjaxResult info = ridService.getInfo(ridgepoleId);
            Object o1 = info.get(AjaxResult.DATA_TAG);
            Ridgepole ridgepole = JSON.parseObject(JSON.toJSONString(o1), Ridgepole.class);
            u.setCommunity(community);
            u.setRidgepole(ridgepole);
        });

        return ucleictasks;

    }

    /**
     * 新增楼栋核酸
     * 
     * @param ucleictask 楼栋核酸
     * @return 结果
     */
    @Override
    public int insertUcleictask(Ucleictask ucleictask)
    {



        Long ridgepoleId = ucleictask.getRidgepoleId();
        AjaxResult countTem = teneMentService.getCount(ridgepoleId);
        Object o2 = countTem.get(AjaxResult.DATA_TAG);
        Long count = JSON.parseObject(JSON.toJSONString(o2), Long.class);
        ucleictask.setTaskPeople(count);
        AjaxResult tenesByRidIdTem = teneMentService.getTenesByRidId(ridgepoleId);
        List<Tenement> tenements = JSON.parseArray(JSON.toJSONString(tenesByRidIdTem.get(AjaxResult.DATA_TAG)), Tenement.class);

        tenements.forEach(t->{
            Tenementsask tenementsask = new Tenementsask();
            tenementsask.setName(t.getName());
            tenementsask.setEndTime(ucleictask.getEndTime());
            tenementsask.setStatus(0);
            tenementsask.setStartTime(ucleictask.getStartTime());
            tenementsask.setCommunityId(ucleictask.getCommunityId());
            tenementsask.setRidgepoleId(ucleictask.getRidgepoleId());
            tenementsask.setTaskName(ucleictask.getTaskName());
            tenementsask.setDetectionPosition(ucleictask.getDetectionPosition());
            tenementsaskMapper.insertTenementsask(tenementsask);
            log.info("http://localhost/login2?id="+t.getId());
        });
        if (null!=ucleictask.getStartRemind()){
            Message<List<Tenement>> build = MessageBuilder.withPayload(tenements).build();


            rocketMQTemplate.syncSend("startRemind-topic:startRemind-tag", build, 20000, 8);
        }
        if(null!= ucleictask.getOverRemind()){
            Message<List<Tenement>> build = MessageBuilder.withPayload(tenements).build();


            rocketMQTemplate.syncSend("overRemind-topic:overRemind-tag", build, 20000, 8);
        }

        return ucleictaskMapper.insertUcleictask(ucleictask);
    }

    /**
     * 修改楼栋核酸
     * 
     * @param ucleictask 楼栋核酸
     * @return 结果
     */
    @Override
    public int updateUcleictask(Ucleictask ucleictask)
    {
        return ucleictaskMapper.updateUcleictask(ucleictask);
    }

    /**
     * 批量删除楼栋核酸
     * 
     * @param ids 需要删除的楼栋核酸主键
     * @return 结果
     */
    @Override
    public int deleteUcleictaskByIds(Long[] ids)
    {
        return ucleictaskMapper.deleteUcleictaskByIds(ids);
    }

    /**
     * 删除楼栋核酸信息
     * 
     * @param id 楼栋核酸主键
     * @return 结果
     */
    @Override
    public int deleteUcleictaskById(Long id)
    {
        return ucleictaskMapper.deleteUcleictaskById(id);
    }



}
